﻿using System;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class WaitList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("IB1") < 0)  //共用K位权限
                {
                    Response.Redirect("unauthorized.aspx");
                }
            }

            lblError.Text = "";
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            GridView1.DataBind();
            if (radStatus.SelectedValue == "C")
            {
                GridView1.Columns[4].Visible = false;
                GridView1.Columns[9].Visible = true;
                GridView1.Columns[10].Visible = true;
            }
            else
            {
                GridView1.Columns[4].Visible = true;
                GridView1.Columns[9].Visible = false;
                GridView1.Columns[10].Visible = false;
            }
        }

        protected void Close_queue(long bk_id, DateTime flt_date)
        {
            //if (Session["ACL"].ToString().IndexOf("IB1") < 0)
            //{
            //    lblError.Text = "Unauthorized!";
            //    return;
            //}

            int ret_code;
            SqlCommand cmd = new SqlCommand();
            try
            {
                cmd.CommandText = "bll_airline_close_waitlist_queue";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@flt_date", flt_date);
                para = cmd.Parameters.AddWithValue("@bk_id", bk_id);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Close queue failed!";
                else
                    lblError.Text = "Error " + ret_code.ToString() + " !";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int i = Convert.ToInt32(e.CommandArgument);
            long bk_id;
            DateTime flt_date;
            bk_id = Convert.ToInt64(((LinkButton)GridView1.Rows[i].Cells[2].Controls[1]).Text);
            flt_date = Convert.ToDateTime(GridView1.Rows[i].Cells[0].Text);
            Close_queue(bk_id, flt_date);
        }
    }
}